# -*- coding: utf-8 -*-
 ##############################################
 ##                                            ##
 ##               Base controller               ##
 ##              for Basiq Empresa              ##
 ##                                             ##
 ##              from Basiq Series              ##
 ##           by Críptidos Digitales            ##
 ##                 GPL (c)2008                 ##
  ##                                            ##
    ##############################################

"""
"""

from decimal import Decimal

import baseModel

import principal
import herramientas
import access
import about
import tips

import administracion
import mantenimiento
import puntoVenta


class Session:

    def __init__(self, **kwds):
        #print "baseController.Session.__init_()"

        if 'app' in kwds:
            self.__app = kwds.pop('app')

        baseModel.session = self

        principal.session = self
        principal.app = self.__app

        herramientas.session = self

        about.session = self
        about.app = self.__app

        access.app = self.__app

        tips.app = self.__app

        administracion.session = self
        administracion.app = self.__app

        mantenimiento.session = self
        mantenimiento.app = self.__app

        puntoVenta.session = self
        puntoVenta.app = self.__app

        self.controllers = []

        self.controllers.append(administracion.Controller())
        self.controllers.append(mantenimiento.Controller())
        self.controllers.append(puntoVenta.Controller())

        self.moneyDecimals = Decimal('0.01')


    def initDb(self):
        # print 'baseController.Session.initDb()'
        baseModel.initDb()

        for controller in self.controllers:
            controller.initDb()

        self.generalTax = Decimal(baseModel.getAttribute(grupo=u'sistema', nombre=u'impuestoGeneral')['valor'])
        self.generalTaxFactor = (self.generalTax + Decimal('100')) / Decimal('100')
        self.holder = baseModel.getFullRol(tipo='propio')
        self.__app.holder = self.holder


    def initForms(self):
        # print 'baseController.Session.initForms()'

        form = principal.Form()
        self.mainForm = form
        self.eventRouter = form

        for controller in self.controllers:
            controller.initForm()

        self.mainForm.showPuntoVenta()


    def incrementRunCount(self):
        try:
            attribute = baseModel.getAttribute(grupo=u'system', nombre=u'runCount')
            attribute['valor'] = u"%s" % (int(attibute['valor']) + 1)
            baseModel.setAttribute(**attribute)
            return True
        except:
            return False


    def isFirstRun(self):
        return not baseModel.getAttribute(grupo=u'system', nombre=u'runCount')['valor']


    def tipsActive(self):
        return not not baseModel.getAttribute(grupo=u'sistema', nombre=u'mostrarTips')['valor']




if __name__ == "__main__":
    pass

